<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>新建收票</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>收票登记单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=piao_shou','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=piao_shou','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>收票日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">收票人</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=shouer&type=user&single=1', '770px', '490px');"></div>
                                <div class="tanxiannei" id="shouer"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=shouer&type=user&single=1', '770px', '490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>收票金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="请输入" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="ratefee" id="ratefee" placeholder="请输入" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>发票明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
<!--                            <button type="button" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>-->
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>合同名称</th>
                            <th><text>*</text>开票单位</th>
                            <th>开票日期</th>
                            <th>发票号码</th>
                            <th><text>*</text>发票类型</th>
                            <th><text>*</text>不含税金额(元)</th>
                            <th><text>*</text>税率(%)</th>
                            <th><text>*</text>税额(元)</th>
                            <th><text>*</text>价税合计(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    $("#commentForm").validate({
        rules: {
            dt: {required: true},
        },
        messages:{
            dt: {required: "请选择"},
        }
    });


    function choose_contract_cai_one(obj){
        var domkey = $(obj).parents('tr').index();
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_contract_cai.php?page=piao_shou&enginid='+enginid+'&domkey='+domkey,'80%','80%');
    }

    function choose_dealcom_one(obj){
        var domkey = $(obj).parents('tr').index();
        openPage('../public/choose_dealcom.php?page=piao_shou&domkey='+domkey,'80%','80%');
    }

    function import_list() {
        openPage('../public/import.php?reveal=piao_shou@importdata', '770px', '490px');
    }


    var pub_piaotype;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_piao_shou_add}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#number").val(data.number);
                $("#dt").val(data.dt);

                if(data.shouer_info.length > 0){
                    tan_tags_init('#shouer', data.shouer_info);
                }

                pub_piaotype = res.data.piaotype
                var piaotype = res.data.piaotype

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" caiid="0" onclick="choose_contract_cai_one(this)" readonly class="layui-input caiid disabled">';
                listAddStr += '</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" onclick="choose_dealcom_one(this)" readonly class="layui-input gonger disabled">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input dt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input nums"></td>';
                listAddStr += '<td><select class="piaotype">';
                for (var key in piaotype){
                    listAddStr += '<option value="'+key+'">'+piaotype[key]+'</option>';
                }
                listAddStr += '</select></td>';
                listAddStr += '<td><input type="number" class="layui-input fee"></td>';
                listAddStr += '<td><input type="number" class="layui-input rate"></td>';
                listAddStr += '<td><input type="number" class="layui-input ratefee"></td>';
                listAddStr += '<td><input type="number" class="layui-input money"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);

                public_upload_file_load('upload_files');
                render_date();
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            parseMoney(data);

            data.field.shouer = tan_tags_getvalue("#shouer", 'user');
            data.field.enginid = get_choose_val('enginid');
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var caiid = $(this).find('.caiid').attr('caiid') || 0;
                    var gonger = $(this).find('.gonger').val();
                    var dt = $(this).find('.dt').val();
                    var nums = $(this).find('.nums').val();
                    var piaotype = $(this).find('.piaotype').val();
                    var fee = $(this).find('.fee').val();
                    var rate = $(this).find('.rate').val();
                    var ratefee = $(this).find('.ratefee').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;

                    if(!gonger){
                        throw new Error('第'+index+'行，请选择开票单位');
                    }
                    if(!piaotype){
                        throw new Error('第'+index+'行，请选择发票类型');
                    }
                    if(!fee){
                        throw new Error('第'+index+'行，请输入不含税金额');
                    }
                    if(!rate){
                        throw new Error('第'+index+'行，请输入税率');
                    }
                    if(!ratefee){
                        throw new Error('第'+index+'行，请输入税额');
                    }
                    if(!money){
                        throw new Error('第'+index+'行，请输入价税合计');
                    }

                    list.push({
                        caiid:caiid,
                        gonger:gonger,
                        dt:dt,
                        nums:nums,
                        piaotype:piaotype,
                        fee:fee,
                        rate:rate,
                        ratefee:ratefee,
                        money:money,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            facade.ajax({url: ask_piao_shou_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });


    $('#list_list').on('input propertychange','.ratefee, .money',function () {
        jisuan_money_all();
    });

    function jisuan_money_all() {
        var allratefee = 0;
        var allmoney = 0;

        $('#list_list tr').each(function (i) {
            var ratefee = parseValidNum($(this).find('.ratefee').val());
            allratefee = parseFloat(allratefee) + parseFloat(ratefee);

            var money = parseValidNum($(this).find('.money').val());
            allmoney = parseFloat(allmoney) + parseFloat(money);
        });


        $("#ratefee").val(parseFormatNum(allratefee,2));
        $("#money").val(parseFormatNum(allmoney,2));
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }



    function choose_enginid_delete() {
        $('#list_list tr').each(function (i) {
            $(this).find('.caiid').attr('caiid', 0).val('');
        })
    }







    var ctrlv_field_name = [
        {field: 'dt', name:'开票日期',type:'date'},
        {field: 'nums', name:'发票号码'},
        {field: 'fee', name:'不含税金额(元)',type:'number'},
        {field: 'rate', name:'税率(%)',type:'number'},
        {field: 'ratefee', name:'税额(元)',type:'number'},
        {field: 'money', name:'价税合计(元)',type:'number'},
        {field: 'explain', name:'备注'},

    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td>';
                str += '<input type="text" caiid="'+list[key].caiid+'" value="" onclick="choose_contract_cai_one(this)" readonly class="layui-input caiid disabled">';
                str += '</td>';
                str += '<td>';
                str += '<input type="text" value="" onclick="choose_dealcom_one(this)" readonly class="layui-input gonger disabled">';
                str += '</td>';
                str += '<td><input type="text" class="layui-input dt bdate_ymd" value="'+list[key].dt+'" readonly></td>';
                str += '<td><input type="text" class="layui-input nums" value="'+list[key].nums+'"></td>';
                str += '<td><select class="piaotype">';
                for (var mm in pub_piaotype){
                    str += '<option value="'+mm+'">'+pub_piaotype[mm]+'</option>';
                }
                str += '</select></td>';
                str += '<td><input type="number" class="layui-input fee" value="'+list[key].fee+'"></td>';
                str += '<td><input type="number" class="layui-input rate" value="'+list[key].rate+'"></td>';
                str += '<td><input type="number" class="layui-input ratefee" value="'+list[key].ratefee+'"></td>';
                str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#list_list").append(str);
            }
        }
        update_refresh_row_func()
        render_date();
        render_from();
    }
</script>
</body>
</html>
